﻿/******************************************************************
 * Copyright 2020-2022  NJUST Co. Ltd.
 * All right reserved. See COPYRIGHT for detailed Information.
 *
 * @file    HttpClientController.h
 * @brief   1、封装Http客户端的典型操作-Get/Post/Put/Delete
 *          2、使用QEventLoop实现异步机制，网络请求操作时不堵塞主线程
 *
 * @author  邹卫军
 * @brief   2022/10/25
 * @history
 ******************************************************************/
#ifndef HTTPCLIENTCONTROLLER_H
#define HTTPCLIENTCONTROLLER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>

/**
 * @brief 基于异步非堵塞的Http协议实现
 */
class HttpClientController : public QObject
{
    Q_OBJECT

public:
    HttpClientController();
    ~HttpClientController();

public:
    // Http的Get操作
    QByteArray GetAsync(QString url, QVariantMap params);
    // Http的Delete操作
    QByteArray DeleteAsync(QString url, QVariantMap params);
    // Http的Put操作
    QByteArray PutAsync(QString url, QByteArray jsonData);
    // Http的Post操作
    QByteArray PostAsync(QString url, QByteArray jsonData);

private:
    QUrl GetUrl(QString baseUrl, QVariantMap params);
    bool ValidateHttpResponse(QNetworkReply *pReply);

private:
    QNetworkAccessManager *pHttpManager;
    QNetworkReply         *pHttpReply;
    QNetworkRequest       *pHttpRequest;

public slots:

signals:
    void sig_SendHttpValidateInfo(QString);

};

#endif // HTTPCLIENTCONTROLLER_H
